//
//  NewsViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/13.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "NewsViewController.h"
#import "LTFNetWorkingTool.h"
#import "LTFNavigationModel.h"
#import "LTFNavigationCollectionViewCell.h"
#import "LTFNewsHeadLineCollectionViewCell.h"
#import "LTFNBACollectionViewCell.h"
#import "LTFExclusiveCollectionViewCell.h"
#import "LTFEntertainmentCollectionViewCell.h"
#import "LTFBeautyCollectionViewCell.h"
#import "LTFJokeCollectionViewCell.h"
#import "LTFHeadLineContentViewController.h"
#import "LTFBeautyContentViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface NewsViewController ()
<
 UICollectionViewDataSource,
 UICollectionViewDelegate,
 LTFNewsHeadLineCollectionViewCellDelegate,
 LTFNBACollectionViewCellDelegate,
 LTFExclusiveCollectionViewCellDelegate,
 LTFEntertainmentCollectionViewCellDelegate,
 LTFBeautyCollectionViewCellDelegate,
 LTFJokeCollectionViewCellDelegate
>
//导航栏
@property (nonatomic, retain) UICollectionView *navigationCollectionView;
//存储导航栏内容的数组
@property (nonatomic, retain) NSMutableArray *navigationArray;
//内容
@property (nonatomic, retain) UICollectionView *contentCollectionView;
//判断是否选中
@property (nonatomic, assign) BOOL isClick;

@property (nonatomic, retain) NSArray *deleteArray;

@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UILabel *label;


@property (nonatomic, assign) NSInteger index;
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 24, WIDTH, 40)];
    
    _myView.backgroundColor = [UIColor redColor];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
    [_myView addSubview:_label];
    _label.textColor = [UIColor whiteColor];
    _label.text = @"網易";
    
    self.isClick = NO;
    [self createView];
    [self createData];
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.view addSubview:_myView];
    
    UITableView *newsTableView = [self.view viewWithTag:1006];
    [newsTableView reloadData];
    
    UITableView *nbaTableView = [self.view viewWithTag:1007];
    [nbaTableView reloadData];
    
    
    UITableView *exclusiveTableView = [self.view viewWithTag:1008];
    [exclusiveTableView reloadData];
    
    
    UITableView *entertainmentTableView = [self.view viewWithTag:1009];
    [entertainmentTableView reloadData];
    
    
    UITableView *beautyTableView = [self.view viewWithTag:1010];
    [beautyTableView reloadData];
    
    
    UITableView *jokeTableView = [self.view viewWithTag:1011];
    [jokeTableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.myView removeFromSuperview];
}
- (void)createView {
    self.deleteArray = @[@"本地", @"网易号", @"跟贴", @"CBA", @"移动互联", @"型男"];
    
#pragma mark *****导航栏******************************
    //导航栏底
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 6, 50);
    //导航栏的滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    //导航栏
    self.navigationCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50) collectionViewLayout:layout];
    [self.view addSubview:self.navigationCollectionView];
    //偏移量
    self.navigationCollectionView.contentOffset = CGPointMake(0, 0);
    //设置代理
    self.navigationCollectionView.delegate = self;
    self.navigationCollectionView.dataSource = self;
    //注册
    [self.navigationCollectionView registerClass:[LTFNavigationCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFNavigationCollectionViewCell class])];
#pragma mark *******************************内容****
    //内容底
    UICollectionViewFlowLayout *contentLayout = [[UICollectionViewFlowLayout alloc] init];
    contentLayout.itemSize = CGSizeMake(WIDTH , HEIGHT - 50);
    //设置底
    contentLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    contentLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    contentLayout.minimumInteritemSpacing = 0;
    contentLayout.minimumLineSpacing = 0;
    //内容栏
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT - 50) collectionViewLayout:contentLayout];
    [self.view addSubview:self.contentCollectionView];
    //设置代理
    self.contentCollectionView.delegate = self;
    self.contentCollectionView.dataSource = self;
    //注册
    //头条
    [self.contentCollectionView registerClass:[LTFNewsHeadLineCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFNewsHeadLineCollectionViewCell class])];
    //nba
    [self.contentCollectionView registerClass:[LTFNBACollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFNBACollectionViewCell class])];
    //独家
    [self.contentCollectionView registerClass:[LTFExclusiveCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFExclusiveCollectionViewCell class])];
    //娱乐
    [self.contentCollectionView registerClass:[LTFEntertainmentCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFEntertainmentCollectionViewCell class])];
    //美女
    [self.contentCollectionView registerClass:[LTFBeautyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFBeautyCollectionViewCell class])];
    //段子
    [self.contentCollectionView registerClass:[LTFJokeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LTFJokeCollectionViewCell class])];
    
    
    
    [self.contentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
    
    //设置滑动方式
    self.contentCollectionView.pagingEnabled = YES;
    
    
}

//collectionView的第一个协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.navigationArray.count;
}
//collectionView的第二个协议
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LTFNavigationModel *model = self.navigationArray[indexPath.row];
    if (collectionView == self.contentCollectionView) {
        if (indexPath.row == 0) {
            LTFNewsHeadLineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFNewsHeadLineCollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }
        else if (indexPath.row == 1) {
            LTFNBACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFNBACollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }
        else if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 8 || indexPath.row == 9 ||  indexPath.row == 12 || indexPath.row == 13 || indexPath.row == 14 || indexPath.row == 18 || indexPath.row == 19 || indexPath.row == 20 || indexPath.row == 24 || indexPath.row == 26 || indexPath.row == 28 || indexPath.row == 29 || indexPath.row == 31 || indexPath.row == 32 || indexPath.row == 33 || indexPath.row == 34 || indexPath.row == 35 || indexPath.row == 36 || indexPath.row == 39 || indexPath.row == 40 || indexPath.row == 41 || indexPath.row == 23 || indexPath.row == 25 || indexPath.row == 27 || indexPath.row == 30 || indexPath.row == 16 || indexPath.row == 17 || indexPath.row == 10 || indexPath.row == 21) {
            LTFExclusiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFExclusiveCollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }
        else if (indexPath.row == 7 || indexPath.row == 11 || indexPath.row == 15 || indexPath.row == 22) {
            LTFEntertainmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFEntertainmentCollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }
        else if (indexPath.row == 38) {
            LTFBeautyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFBeautyCollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }
        else if (indexPath.row == 37) {
            LTFJokeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFJokeCollectionViewCell class]) forIndexPath:indexPath];
            cell.string = model.tid;
            cell.delegate = self;
            return cell;
        }

        else {
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
            return cell;
        }
        
        
    }
    else if (collectionView == self.navigationCollectionView) {
        LTFNavigationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LTFNavigationCollectionViewCell class]) forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        if (indexPath.row == self.index) {
            cell.titleLabel.textColor = [UIColor redColor];
        }
        else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        LTFNavigationModel *model = self.navigationArray[indexPath.row];
        cell.titleLabel.text = model.tname;
        return cell;
    }
    else {
        return nil;
    }
}
//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.navigationCollectionView) {
        self.contentCollectionView.contentOffset = CGPointMake(WIDTH * indexPath.row, 0);
        LTFNavigationCollectionViewCell *newCell = (LTFNavigationCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        newCell.titleLabel.textColor = [UIColor redColor];
        
        LTFNavigationCollectionViewCell *oldCell = (LTFNavigationCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        oldCell.titleLabel.textColor = [UIColor lightGrayColor];
        self.index = indexPath.row;
        
        [self.navigationCollectionView reloadData];
    }
}
//上下联动方法
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.contentCollectionView) {
        CGFloat x = self.contentCollectionView.contentOffset.x;
        
        if (x >= WIDTH * (self.navigationArray.count - 6)) {
            self.navigationCollectionView.contentOffset = CGPointMake(WIDTH * (self.navigationArray.count - 6) / 6, 0);
        }
        else if (x <= WIDTH) {
            self.navigationCollectionView.contentOffset = CGPointMake(x / 6, 0);
        }
        else {
        self.navigationCollectionView.contentOffset = CGPointMake(x / 6, 0);
        }
        self.index = (NSInteger)(x / WIDTH);
        [self.navigationCollectionView reloadData];
    }
    
}

#pragma mark 对地址进行解析
- (void)createData {
    //初始化数组
    self.navigationArray = [NSMutableArray array];
    NSString *str = @"http://c.3g.163.com/nc/topicset/android/subscribe/manage/listspecial.html";
    //调用解析方法
    [LTFNetWorkingTool toolWithUrl:str block:^(id result) {
        NSDictionary *dic = result;
        NSArray *tList = dic[@"tList"];
        //调用model
        self.navigationArray = [LTFNavigationModel BaseModelByArray:tList];
        for (NSInteger i = 0; i < self.navigationArray.count; i++) {
            LTFNavigationModel *model = self.navigationArray[i];
            if (model.headLine == 1) {
                [self.navigationArray exchangeObjectAtIndex:0 withObjectAtIndex:i];
            }
            
            
        }
        for (NSInteger i = 0; i < self.navigationArray.count; i++) {
            LTFNavigationModel *model = self.navigationArray[i];
            for (NSString *string in self.deleteArray) {
                if ([model.tname isEqualToString:string]) {
                    [self.navigationArray removeObject:model];
                    break;
                }
            }
        }
        for (NSInteger i = 0; i < self.navigationArray.count; i++) {
            LTFNavigationModel *model = self.navigationArray[i];
            if ([model.tname isEqualToString:@"美女"]) {
                [self.navigationArray removeObject:model];
            }
        }
        for (NSInteger i = 0; i < self.navigationArray.count; i++) {
            LTFNavigationModel *model = self.navigationArray[i];
            if ([model.tname isEqualToString:@"萌宠"]) {
                [self.navigationArray removeObject:model];
            }
        }
        for (NSInteger i = 0; i < self.navigationArray.count; i++) {
            LTFNavigationModel *model = self.navigationArray[i];
            if ([model.tname isEqualToString:@"热点"]) {
                [self.navigationArray removeObject:model];
            }
        }
        
        [self.navigationCollectionView reloadData];
        [self.contentCollectionView reloadData];
        
    }];
}
- (void)sendString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}
- (void)sendNBAString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}

- (void)sendExclusiveString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}
- (void)sendEntertainmentString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}
- (void)sendBeautyString:(NSString *)string {
    LTFBeautyContentViewController *beautyContentVC = [[LTFBeautyContentViewController alloc] init];
    [self.navigationController pushViewController:beautyContentVC animated:YES];
    beautyContentVC.string = string;
    [beautyContentVC release];
    
}
- (void)sendJokeString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}

- (void)sendHeadString:(NSString *)string {
    LTFBeautyContentViewController *beautyContentVC = [[LTFBeautyContentViewController alloc] init];
    [self.navigationController pushViewController:beautyContentVC animated:YES];
    beautyContentVC.string = string;
    [beautyContentVC release];
   
}
- (void)sendNBAHeadString:(NSString *)string {
    if (string.length != 16) {
        LTFBeautyContentViewController *beautyContentVC = [[LTFBeautyContentViewController alloc] init];
        [self.navigationController pushViewController:beautyContentVC animated:YES];
        beautyContentVC.string = string;
        [beautyContentVC release];
    }
    else {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
    }
}
- (void)sendHeadExclusiveString:(NSString *)string {
    if (string.length != 16) {
        LTFBeautyContentViewController *beautyContentVC = [[LTFBeautyContentViewController alloc] init];
        [self.navigationController pushViewController:beautyContentVC animated:YES];
        beautyContentVC.string = string;
        [beautyContentVC release];
    }
    else {
        LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
        [self.navigationController pushViewController:headLineContentVC animated:YES];
        headLineContentVC.string = string;
        [headLineContentVC release];
    }
}
- (void)sendHeadEntertainmentString:(NSString *)string {
    LTFHeadLineContentViewController *headLineContentVC = [[LTFHeadLineContentViewController alloc] init];
    [self.navigationController pushViewController:headLineContentVC animated:YES];
    headLineContentVC.string = string;
    [headLineContentVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
